/**
 * @description 用于 格式化 审核状态
 * @date 2020-10-19
 * @export
 * @param {number} value
 * @returns
 */
export function fmtStatus(value: number) {
  if (value === 1) return 'status-yes'
  else if (value === 2) return 'status-no'
  else if (value === 0) return 'status-pending'
}

// 设置登录时间, 有效期 7天
export async function setLoginTime() {
  setStorageSync('TIME', Date.now() + 86400000 * 7)
}

export async function setStorageSync(key: string, value: any) {
  try {
    await uni.setStorageSync(key, value)
    return true
  } catch (e) {
    console.error(`设置缓存 ${key} 失败`)
  }
}

export async function getStorageSync(key: string) {
  try {
    const value = await uni.getStorageSync(key)
    if (value) {
      return value
    } else {
      return ''
    }
  } catch (e) {
    console.error(`获取缓存 ${key} 失败`)
  }
}

export function setHttpIMG(url: string) {
  return process.env.VUE_APP_IMG_URL + url
}

// 获取当前页面 url
export function getCurrentPageUrl(){
  const pages = getCurrentPages()    //获取加载的页面
  const currentPage = pages[pages.length-1]    //获取当前页面的对象
  const url = currentPage.route    //当前页面url
  return url
}

// 用于选择 身份 - 角色 后递归出选择的数据
export function findIdentityTree(data: AnyArray, list: AnyArray, findkey = 'code', key = 'value') {
  let arr: AnyArray = []
  let index = 0
  if (data.length === 0) return arr
  function find(data: AnyArray, list: AnyArray, findkey: string, key: string) {
    const findItem = list.find(item => {
      return item[findkey] === data[index][key]
    })
    if (findItem !== undefined) {
      arr.push(findItem)
      if (findItem.children && findItem.children.length > 0) {
        index++
        find(data, findItem.children, findkey, key)
      }
    }
  }
  find(data, list, findkey, key)
  return arr
}
